from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from database.config import get_db
from models.model import *
from models.base import *

router = APIRouter()

@router.get("/post_list")
async def post_list(db: Session = Depends(get_db)):
    post_list = []
    queryList = db.query(Post).all()
    for item in queryList:
        publish_time = item.post_publish_time.strftime('%Y-%m-%d')
        post_list.append({
            "post_id": item.post_id,
            "post_title": item.post_title,
            "post_author": item.post_author,
            "post_author_avatar": item.post_author_avatar,
            "post_publish_time": publish_time,
            "post_abstract": item.post_abstract,
            "post_pic": item.post_pic,
            "post_like_num": item.post_like_num,
            "post_comment_num": item.post_comment_num
        })
    res_data = {"post_list": post_list}
    return Success(code = 200, msg = "success", res_data = res_data)

@router.get("/post/{post_id}")
async def community_post(post_id: str, db: Session = Depends(get_db)):
    post_comment_list = []
    post = db.query(Post).filter(Post.post_id == post_id).first()
    commentList = db.query(Comment).filter(Comment.post_id == post_id).all()
    for comment in commentList:
        post_comment_list.append({
            "comment_id": comment.comment_id,
            "comment_author": comment.comment_author,
            "comment_author_avatar": comment.comment_author_avatar,
            "comment_time": comment.comment_time.strftime('%Y-%m-%d'),
            "comment_content": comment.comment_content,
            "comment_like_num": comment.comment_like_num,
            "comment_dislike_num": comment.comment_dislike_num
        })
    res_data = {
        "post_id":post.post_id,
        "post_title":post.post_title,
        "post_author":post.post_author,
        "post_author_avatar":post.post_author_avatar,
        "post_publish_time":post.post_publish_time.strftime('%Y-%m-%d'),
        "post_content":post.post_content,
        "post_like_num":post.post_like_num,
        "post_comment_num":post.post_comment_num,
        "post_comment_list":post_comment_list
    }
    return Success(code = 200, msg = "success", res_data = res_data)


@router.get("/hot_list")
async def hot_list(db: Session = Depends(get_db)):
    hotPost = db.query(HotPost).all()
    hotComment = db.query(HotComment).all()

    hot_post_list = []
    hot_comment_list = []
    for item in hotPost:
        hot_post_list.append({
            "post_id": item.post_id,
            "post_title": item.post_title,
            "browse_num": item.browse_num
        })
    for item in hotComment:
        comment_time = item.comment_time.strftime('%Y-%m-%d')
        hot_comment_list.append({
            "comment_id": item.comment_id,
            "comment_author": item.comment_author,
            "comment_author_avatar": item.comment_author_avatar,
            "comment_time": comment_time,
            "comment_content": item.comment_content,
            "comment_like_num": item.comment_like_num,
            "comment_dislike_num": item.comment_dislike_num,
        })
    res_data = {
        "hot_post_list": hot_post_list,
        "hot_comment_list": hot_comment_list
    }
    return Success(code = 200, msg = "success", res_data = res_data)

